package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.Iterator;
import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.DomainHelper;
import ch.fhnw.qualiscan.application.pages.EditSurvey;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
/**
 * This tab is shown for both participants and coordinators to fillout the domains.
 */
public class EditSurveyTab1 extends VerticalLayout {
	
	public EditSurvey editSurvey;
	public Label currentStage;
	
	private Panel panel1;
	private Panel panel2;
	
	public EditSurveyTab1(EditSurvey editSurvey) {
		this.editSurvey = editSurvey;
		buildTab();
	}
	
	public void buildTab() {
		this.removeAllComponents();
		
		if(editSurvey.sur.getRole().getId() == Config.COORDINATOR_ID) {
			this.addComponent(buildPanel1());
		}
		
		this.addComponent(buildPanel2());
	}
	
	public Panel buildPanel1() {
		panel1 = new Panel();
		panel1.addComponent(editSurvey.textHelper.h2(TM.get("fillout.coordinatorrole.title")));
		panel1.addComponent(editSurvey.textHelper.span(TM.get("fillout.coordinatorrole.title.desc")));
		
		final ComboBox fillOutRole = new ComboBox();
		List<Role> roleList = editSurvey.roleModel.getParticipantRoles();
		
		for (int i = 0; i < roleList.size(); i++) {
			fillOutRole.addItem(roleList.get(i));
			fillOutRole.setItemCaption(roleList.get(i), editSurvey.userModel.getRoleText(roleList.get(i), editSurvey.userSession.getLanguage()).getTitle());
		}
		
		if (editSurvey.sur.getFillOutRole() != null) {
			fillOutRole.setValue(editSurvey.sur.getFillOutRole());
		}
		
		panel1.addComponent(fillOutRole);
		
		Button saveButton = new Button(TM.get("action.save"));
		saveButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//Save fillOutRole
				if (fillOutRole.getValue() != null) {
					editSurvey.sur.setFillOutRole((Role) fillOutRole.getValue());
					editSurvey.surveyModel.updateSur(editSurvey.sur);
					NotificationLib.showSuccess(TM.get("action.saved"), null);
					panel2.setEnabled(true);
				}
				else {
					event.getButton().setEnabled(true);
					event.getButton().setCaption(TM.get("fillout.save"));
					NotificationLib.showError(TM.get("user.error.role"), "");
					return;
				}
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		
		HorizontalLayout saveButtonWrap = new HorizontalLayout();
		saveButtonWrap.setMargin(true, false, false, false);
		
		saveButtonWrap.addComponent(saveButton);
		
		addComponent(panel1);
		panel1.addComponent(saveButtonWrap);
		
		return panel1;
	}
	
	public Panel buildPanel2() {
		
		panel2 = new Panel();
		panel2.addComponent(editSurvey.textHelper.h2(TM.get("editsurvey.tab1.ownprogresspanel.title")));
		
		Button domainHelp = new Button(TM.get("helper.domainhelp"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editSurvey.openModal(new DomainHelper());
			}
		});
		domainHelp.setStyleName(Button.STYLE_LINK);
		
		CssLayout spacer = new CssLayout();
		spacer.setHeight("15px");
		spacer.setWidth("100%");
		panel2.addComponent(spacer);
		panel2.addComponent(domainHelp);
		
		boolean isSurveyFinished = true;
		CssLayout surveyFinishedWrap = new CssLayout();
		surveyFinishedWrap.setVisible(false);
		surveyFinishedWrap.setMargin(true);
		surveyFinishedWrap.setWidth("800px");
		surveyFinishedWrap.setStyleName("box-ok");
		
		Label surveyFinished = new Label();
		surveyFinishedWrap.addComponent(surveyFinished);

		panel2.addComponent(surveyFinishedWrap);
		
		Language l = editSurvey.userSession.getLanguage();	
		Iterator it = editSurvey.currentSurvey.getDomains().iterator();	
		while(it.hasNext()) {

			Domain d = (Domain)it.next();
			DomainText dt = editSurvey.domainModel.getDomainText(d, l);
			
			int nQuestions = 0;
			Iterator<Question> questionIterator= d.getQuestions().iterator();
			while (questionIterator.hasNext()) {
				Question q = questionIterator.next();
				if (q.getSubquestions().size() > 0) {
					nQuestions = nQuestions + q.getSubquestions().size();
				}
				else {
					nQuestions++;
				}
			}
			
			int totalAnswers = editSurvey.domainModel.getNumberOfAnswersForSurAndDomain(editSurvey.sur, d);
			float progress = (float)totalAnswers/(float)nQuestions;
			if (progress != 1.0) {
				isSurveyFinished = false;
			}

			// container
			HorizontalLayout container = new HorizontalLayout();
			container.setSpacing(true);
			container.setMargin(true, false, false, false);
			
			//Title
			Label domainTitle = editSurvey.textHelper.span(dt.getText());
			domainTitle.setWidth("300px");
			
			//Indicator
			ProgressIndicator progressIndicator = new ProgressIndicator();
			progressIndicator.setValue(progress);
			progressIndicator.setWidth("");
			progressIndicator.setPollingInterval(180000);
			
			//Fill in Button
			Button actionButton = new Button(TM.get("editsurvey.fillout"), new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					Domain domain = (Domain)event.getButton().getData();
					editSurvey.urlLib.setFragment("FillOut/"+editSurvey.sur.getId()+"/"+domain.getId()+"/", true);
				}
			});
			actionButton.setData(d);
			actionButton.setIcon(Config.ICON_EDIT);
			
			if(editSurvey.currentSurvey.getStage().getId() == Config.STAGE_EVALUATION) {
				actionButton.setEnabled(false);
			}
			
			container.addComponent(domainTitle);
			container.addComponent(progressIndicator);
			
			container.addComponent(actionButton);
			container.setComponentAlignment(actionButton, Alignment.MIDDLE_CENTER);
			
			container.setComponentAlignment(domainTitle, Alignment.MIDDLE_CENTER);
			container.setComponentAlignment(progressIndicator, Alignment.MIDDLE_CENTER);

			panel2.addComponent(container);
		}
		
		if (isSurveyFinished) {
			if(editSurvey.currentSurvey.getStage().getId() == Config.STAGE_SURVEY) {
				surveyFinished.setValue(TM.get("editsurvey.tab1.surveyfinished"));
				surveyFinishedWrap.setVisible(true);
			} else {
				surveyFinished.setValue(TM.get("editsurvey.tab1.surveyfinished.results"));
				surveyFinishedWrap.setVisible(true);
			}
		}
	
		if(editSurvey.sur.getRole().getId() == Config.COORDINATOR_ID && editSurvey.sur.getFillOutRole() == null)
			panel2.setEnabled(false);
		
		return panel2;
	}	
	
}	
